package com.lanou3g.musicenjoy.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.adapter.SearchingSingleSongAdapter;
import com.lanou3g.musicenjoy.entity.SearchSingleSong;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by 辛鑫 on 15/10/21.
 * 单曲界面
 */
public class SearchingSingleSongFragment extends Fragment implements AdapterView.OnItemClickListener, ExpandableListView.OnGroupExpandListener {
    private TextView songNum;
    private RelativeLayout singlesongLayout;
    private ExpandableListView singlesongList;
    private String newUrl;
    private RequestQueue requestQueue;
    private List<SearchSingleSong.DataEntity> data;
    private SearchingSingleSongAdapter adapter;
    //记录当前展开的列表的位置
    private int expPosition;

    public static SearchingSingleSongFragment passData(Context mContext, String str) {
        SearchingSingleSongFragment fragment = new SearchingSingleSongFragment();
        Log.i("wangshimei", str + " 88888");
        Bundle bundle = new Bundle();
        bundle.putString("str", str);
        fragment.setArguments(bundle);
        return fragment;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.searching_singlesong, null);
        songNum = (TextView) view.findViewById(R.id.song_count);
        singlesongLayout = (RelativeLayout) view.findViewById(R.id.singlesong_layout);
        singlesongList = (ExpandableListView) view.findViewById(R.id.singlesong_list);
        singlesongList.setOnGroupExpandListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


    }

    @Override
    public void onResume() {
        super.onResume();
        requestQueue = VolleySingleton.getInstance(getActivity()).getRequestQueue();
        adapter = new SearchingSingleSongAdapter(getActivity());
        Bundle bundle = getArguments();
        String url = bundle.getString("str");
        Log.i("Sysout", "SearchingSingleSongFragment-url:" + url);
        newUrl = "http://api.dongting.com/misc/search/song?uid=862136025609255&hid=6143071666190381&from=android&resolution=720x1280&net=2&api_version=1.0&utdid=U157OqK3fvMBAPX2Buw9EsPo&page=1&longitude=0.0&user_id=0&splus=4.2.2%252F17&tid=0&client_id=cee01a9547fd6d29d9ea526f9ae6936a&f=f3030&os=4.2.2&app=ttpod&rom=Coolpad%252FCoolpad8297W%252FCoolpad8297W%253A4.2.2%252FJDQ39%252F4.2.043.P0.141223.8297W%253Auser%252Frelease-keys&alf=alf702006&imei=862136025609255&cpu=mt6582&agent=none&size=50&v=v8.2.0.2015091720&s=s200&address=%E8%BE%BD%E5%AE%81%E7%9C%81%E5%A4%A7%E8%BF%9E%E5%B8%82%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA%E4%BA%94%E4%B8%80%E8%B7%AF314&ram=2010676+kB&q=" + url + "&active=0&latitude=0.0&language=zh&mid=Coolpad%2B8297W&cpu_model=MT6592";
        Log.i("searching_url", newUrl + "0000");
        getInternetData();
        singlesongList.setOnItemClickListener(this);
    }

    //从网络上拉取数据
    private void getInternetData() {
        StringRequest request = new StringRequest(newUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Gson gson = new Gson();
                SearchSingleSong entity = gson.fromJson(s, SearchSingleSong.class);
                if (entity != null && entity.getData() != null) {
                    data = entity.getData();
                    singlesongList.setAdapter(adapter);
                    adapter.setDatas(data);
                    songNum.setText(data.size() + "");
                    singlesongLayout.setVisibility(View.VISIBLE);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        requestQueue.add(request);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        getFragmentManager()
                .beginTransaction()
                .remove(SearchingSingleSongFragment.this)
                .commit();

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    //最外层列表展开时会调用的方法
    @Override
    public void onGroupExpand(int groupPosition) {
        if (expPosition != groupPosition) {
            singlesongList.collapseGroup(expPosition);
            expPosition = groupPosition;
        }
    }
}
